package zozopo.manual;

import java.sql.Connection;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.SessionAware;


import zozopo.database.BusiDatabaseManager;

import zozopo.manual.operations.ContentImagesOS;
import zozopo.manual.operations.ContentImagesTB;
import zozopo.manual.operations.ContentOS;
import zozopo.manual.operations.ContentTB;

import zozopo.user.operations.UserTB;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

public class ReleaseAction extends ActionSupport implements Action ,SessionAware,ServletRequestAware{
	
	private Map<Object, Object> session;
	private HttpServletRequest request;
	//输入
	private int contentId;
	private String title;
	private String context1;
	private String kind;
	private String cnbrand;
	private String enbrand;
	
	//输出
	private String operationResult;


	public String getKind(){
		return kind;
	}

	
	private int id;
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String execute(){
		UserTB user = (UserTB) session.get("userInfo");
		
		String ip =getIpAddr(request);//获取IP
		
		BusiDatabaseManager databaseConnection = new BusiDatabaseManager();
		Connection conn = databaseConnection.getConnection();
		try{
			ContentOS contentOS = new ContentOS(conn);
			ContentImagesOS contentImagesOS = new ContentImagesOS(conn);
			
			ContentTB content = contentOS.get(contentId);
			ContentImagesTB contentImages = contentImagesOS.getByContentIdOrCreatByUserId(contentId,user.getId_user());
			
			content.setTitle(title);
			content.setCnbrand(cnbrand);
			content.setEnbrand(enbrand);
			content.setKind(kind);
			content.setUsername(user.getName_user());
			content.setIp(ip);
			
			
			content.setContext(context1);
			content.setImage(contentImages.getSmall_image_url_1());
			
			if(contentOS.update(content)){
				contentOS.updateDraft(content.getId(),false);
				operationResult = "保存成功!";
			}else{
				operationResult = "系统存在问题，请联系管理员!";
			}
			
			id = content.getId();
		}finally{
			databaseConnection.releaseConnection(conn);
		}
		
		
		
		
		return SUCCESS;
	}
	


	public void validate() {
		

		
		if(title.length() < 5){
			addFieldError("errormessage", "标题长度不能少于5个字符.请重新输入!");
			return;
		}
		
		//if(Validate.isSpecialCharacters(brand)){
		//	addFieldError("errormessage", "品牌长度不能包含特殊字符  ;\"'\\`|$");
		//	return;
		//	}

		UserTB user = (UserTB) session.get("userInfo"); 
		if(user == null){
			addFieldError("errormessage", "用户未登陆请保存需要发布数据.登陆后再重新发布");
		}
		
		BusiDatabaseManager databaseConnection = new BusiDatabaseManager();
		Connection conn = databaseConnection.getConnection();
		try{
			ContentOS contentOS = new ContentOS(conn);
			ContentTB content = contentOS.get(contentId);
			if(content.getUserid() != user.getId_user()){
				addFieldError("errormessage", "非当前文章用户,请刷新使用现有用户进行发布");
			}
		}finally{
			databaseConnection.releaseConnection(conn);
		}
		
	
	}
	
	
	public void setSession(Map arg0) {
		// TODO Auto-generated method stub
		session = arg0;
	}

	

	/**
     * 获得客户端真是ip
     */
    public static String getIpAddr(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");    
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {    
            ip = request.getHeader("Proxy-Client-IP");    
        }    
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {    
            ip = request.getHeader("WL-Proxy-Client-IP");    
        }    
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {    
            ip = request.getRemoteAddr();    
     }
		return ip; 
    }

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getCnbrand() {
		return cnbrand;
	}

	public void setCnbrand(String cnbrand) {
		this.cnbrand = cnbrand;
	}

	public String getEnbrand() {
		return enbrand;
	}

	public void setEnbrand(String enbrand) {
		this.enbrand = enbrand;
	}



	public void setKind(String kind) {
		this.kind = kind;
	}

	public String getOperationResult() {
		return operationResult;
	}

	public void setOperationResult(String operationResult) {
		this.operationResult = operationResult;
	}

	public String getContext1() {
		return context1;
	}

	public void setContext1(String context1) {
		this.context1 = context1;
	}



	public int getContentId() {
		return contentId;
	}

	public void setContentId(int contentId) {
		this.contentId = contentId;
	}

	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		// TODO Auto-generated method stub
		request = arg0;
	}

}
